package com.yonyou.pmclouds.plan.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.ProjectPlanVO;
import com.yonyou.pmclouds.plan.mapper.ProjectPlanMapper;
import org.apache.commons.lang3.StringUtils;

public class PlanCodeGenProcessor implements SingleProcessor<ProjectPlanVO> {
    private ProjectPlanVO parent;
    private ProjectPlanMapper mapper;

    public PlanCodeGenProcessor(ProjectPlanVO parent, ProjectPlanMapper mapper) {
        this.parent = parent;
        this.mapper = mapper;
    }

    @Override
    public void process(ProjectPlanVO vo, ProjectPlanVO orig) throws BusinessException {
        if (!StringUtils.isEmpty(vo.getPkParent())) {
            // 根据parent生成编码
            String code = parent.getScode();
            code += '.';
            int no = parent.getChildCount();
            code += no;
            vo.setScode(code);
            vo.setSeq(no);
        } else {
            // 查询数据库，生成编码
            int no = mapper.countRootPlanOfProject(vo.getPkProject());
            vo.setScode(Integer.toString(no + 1));
            vo.setSeq(no + 1);
        }
    }
}
